//
// Created by Jisam on 01/10/2024 6:59 PM.
// Solution of  c
#include <bits/stdc++.h>

#define  int long long
#define  endl "\n"
using namespace std;


signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int t = 0;
    cin >> t;
    while (t--) {
        string s;
        cin >> s;
        map<int, int> mp;
        int n = s.size();
        vector<int> a(n), p(n), c(n), a2, p2, c2;
        int ka = 0, pa = 0, ca = 0;
        for (int i = 0; i < s.length(); i++) {
            mp[s[i]]++;
            if (s[i] == 'A') {
                a[ka++] = i;
            } else if (s[i] == 'P') {
                p[pa++] = i;

            } else if (s[i] == 'C') {
                c[ca++] = i;

            }
        }
        int _min = min({mp['A'], mp['P'], mp['C']});
        int cnt = 0;

        for (int i = 0; i < n; i++) {

            if (cnt > _min) break;
            int ta = s.find('A');
            int tp = s.find('P');
            while (tp <= ta || s[tp] != 'P') tp++;
            int tc = s.find('C');
            while (tc <= tp || s[tp] != 'C') tc++;
            cout << ta << " " << tp << " " << tc << endl;
            s[ta] = ' ';
            s[tp] = ' ';
            s[tc] = ' ';
            a2.push_back(ta);
            p2.push_back(tp);
            c2.push_back(tc);
            cnt++;
            cout << endl;
        }
        cout << s;
    }
    return 0;
}